/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.common.template.helper;

import com.apache.api.vo.ParamsVo;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.portal.common.template.FreemarkerHelper;
import com.apache.portal.common.util.FileOperate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成配置实现类
 *
 * @author dyhou 创建时间：2016-9-30
 */
public class ConfigTempManager extends SupperTemplateManager {

    @SuppressWarnings("unchecked")
    public boolean createConfig(Map<String, Object> param) {
        boolean mark = false;
        List<PubSortInfo> list = (List<PubSortInfo>) param.get("sortList");
        String jarPath = String.valueOf(param.get("packPath"));
        String projectName = String.valueOf(param.get("projectName"));
        String xmlPath = String.valueOf(param.get("filePath"));
        //		String sysName = jarPath.substring(jarPath.lastIndexOf(".") + 1);//com.iussoft.utr中的utr
        String sysName = String.valueOf(param.get("sysCode"));
        try {
            createSpring(list, jarPath, projectName, xmlPath, sysName);
            createHibernateAndMyibatis(list, jarPath, projectName, xmlPath, sysName);
            mark = true;
        } catch (Exception e) {
            log.error("生成配置失败", e);
        }
        return mark;
    }

    private void createSpring(List<PubSortInfo> list, String jarPath, String projectName,
            String filePath, String sysName) throws Exception {
        String xmlPath = filePath + "/" + projectName + "/src/main/resources/spring/";
        FileOperate.getInstance().newCreateFolder(xmlPath);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("packPath", jarPath);
        data.put("reqName", sysName);
        data.put("sysName", FileOperate.getInstance().toUpperFirst(sysName));
        data.put("sorts", list);
        String result = FreemarkerHelper.instance()
                .getTemplateStr(data, "config/spring/applicationContext.inc");
        FileOperate.getInstance()
                .newCreateFile(xmlPath + "applicationContext-" + sysName + ".xml", result);
    }

    @SuppressWarnings("rawtypes")
    private void createHibernateAndMyibatis(List<PubSortInfo> list, String jarPath,
            String projectName, String filePath, String sysName) throws Exception {
        String xmlPath = filePath + "/" + projectName + "/src/main/resources/";
        //		FileOperate.getInstance().newCreateFolder(xmlPath + "hibernate/config/");
        FileOperate.getInstance().newCreateFolder(xmlPath + "ibatis/sqlmaps/");
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("packPath", jarPath);
        ParamsVo vo = new ParamsVo();
        PubMetadata metadata = new PubMetadata();
        for (PubSortInfo map : list) {
            metadata.setSortId(String.valueOf(map.getSortId()));
            vo.setObj(metadata);
            List mts = null;// (List) metadataManager.getList(vo);
            data.put("metadatas", mts);
            data.put("sort", map);
            String result = FreemarkerHelper.instance()
                    .getTemplateStr(data, "config/ibatis/ObjNameMapper.inc");
            result = result.replaceAll("###", "#{");
            FileOperate.getInstance()
                    .newCreateFile(xmlPath + "ibatis/sqlmaps/" + map.getObjName() + "Mapper.xml",
                            result);
            //			result = FreemarkerHelper.instance().getTemplateStr("config/hibernate/ObjName.inc", data);
            //			FileOperate.getInstance().newCreateFile(
            //					xmlPath + "hibernate/config/" + String.valueOf(map.get("objName")) + ".hbm.xml", result);
        }
    }

}
